﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace AForge.Imaging.Filters
{    
    [Serializable] public class AForgeBaseNotifyPropertyChanged : INotifyPropertyChanged
    {
        [NonSerialized]
        protected bool _suspendPropertyChangedEvent;

        public bool SuspendPropertyChangedEvent
        {
            get { return _suspendPropertyChangedEvent; }
            set { _suspendPropertyChangedEvent = value; }
        }

        [field:NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;
        
        public bool ConvenientSet<T>(ref T val, T newVal, string propertyName) 
        {
            if ((newVal == null || !newVal.Equals(val)) ||
                (val == null || !val.Equals(newVal)))
            {
                val = newVal;
                OnPropertyChanged(propertyName);
                return true;
            }

            return false;
        }

        protected void OnPropertyChanged(string propertyName)
        {
            if (!_suspendPropertyChangedEvent && PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public void RaisePropertyChanged(string propertyName)
        {
            OnPropertyChanged(propertyName);
        }
    }
}
